package com.zyw.service.procurement.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zyw.entity.Order;
import com.zyw.entity.ProcurementOrders;
import com.zyw.from.ProcurementFrom;
import com.zyw.mapper.procurement.ProcurementOrdersMapper;
import com.zyw.service.order.OrderService;
import com.zyw.service.procurement.ProcurementOrdersService;
import com.zyw.util.UnitConversionUtil;
import com.zyw.vo.LayUITableVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;


@Slf4j
@Service
public class ProcurementOrdersServiceImpl implements ProcurementOrdersService {

    @Resource
    public ProcurementOrdersMapper procurementOrdersMapperl;

    @Autowired
    public OrderService orderService;


    @Override
    public LayUITableVO<List<ProcurementOrders>> findPage(Map<String, Object> map) {
        ProcurementFrom from = (ProcurementFrom) map.get("from");

        PageHelper.startPage((int) map.get("page"),(int) map.get("limit"));

        Example example = new Example(ProcurementOrders.class);
        Example.Criteria c = example.createCriteria();


        if (! "".equals(from.getOrderId()) && null!=from.getOrderId()){
            c.andLike("id","%"+from.getOrderId()+"%");
        }
        if (! "".equals(from.getPrincipal()) && null!=from.getPrincipal()){
            c.andLike("principal","%"+from.getPrincipal()+"%");
        }
        List<ProcurementOrders> list = procurementOrdersMapperl.selectByExample(example);

        PageInfo<ProcurementOrders> pageInfo = new PageInfo<>(list);

        LayUITableVO<List<ProcurementOrders>> vo = new LayUITableVO<>();
        vo.setCount(pageInfo.getTotal());
        vo.setData(pageInfo.getList());

        return vo;
    }

    @Override
    public ProcurementOrders selectById(String id) {
        ProcurementOrders procurementOrders = procurementOrdersMapperl.selectByPrimaryKey(id);
        procurementOrders.setPrincipalDate(UnitConversionUtil.conversionDate(procurementOrders.getPrincipalDate()));
        return procurementOrders;
    }

    @Override
    @Transactional
    public void save(ProcurementOrders conversion) {
        ProcurementOrders procurementOrders = procurementOrdersMapperl.selectByPrimaryKey(conversion.getId());

        if (procurementOrders==null){
            conversion.setProcurementStatus(0);
            conversion.setProductionStatus(0);
            procurementOrdersMapperl.insert(conversion);
            Order order = new Order();
            order.setId(conversion.getId());
            order.setStatus(2);
            orderService.update(order);
        }else {
            procurementOrdersMapperl.updateByPrimaryKeySelective(conversion);
        }
    }

    @Override
    @Transactional
    public void updateStatus(ProcurementOrders procurementOrders) {

        procurementOrdersMapperl.updateByPrimaryKeySelective(procurementOrders);
    }
}
